package com.deer.wms.ware.task.model.pushErp;

import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.SO.SoDetail;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.enums.ErpType;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.deer.wms.project.root.util.CollectionUtils.convertMap;

/**
 * @Author: xu
 * @Date: 2022/06/16/22:23
 * @Description:
 */
public  abstract class AbstractPushErpBean {
    @Autowired
    private CodeRuleService codeRuleService;

    //获取类型
    protected abstract ErpType gainType();

    /**
     * 前置操作
     * @param soMaster
     * @param soDetail
     * @return
     */
    protected abstract Boolean front(SoMaster soMaster, List<SoDetail> soDetail);
    /**
     * 字段映射
     * @param soMaster
     * @param soDetails
     */
    public OrderToErpBO EnterBeanAndPush(SoMaster soMaster, List<SoDetail> soDetails) {
        OrderToErpBO orderToErpBO = new OrderToErpBO();
        List<OrderDetailBO> details = new ArrayList<>();
        orderToErpBO.setBillTypeId(soMaster.getSoType());
        orderToErpBO.setSourceBillTypeId(soMaster.getBillType());
        orderToErpBO.setBillDate(DateUtils.getNowDateString()); //日期
        orderToErpBO.setBillNo(codeRuleService.generateCodeByRedis("OutPut"));
        for (SoDetail soDetail : soDetails) {
            OrderDetailBO orderDetailBO = new OrderDetailBO();
            orderDetailBO.setDetailNo(soDetail.getDetailNo());
            orderDetailBO.setSourceBillId(soDetail.getErpBillId()); //源单id
            orderDetailBO.setSourceBillDetailRowIndex(soDetail.getBomSplit()); //行号
            orderDetailBO.setBatchNo(soDetail.getBatchName()); //批号
            orderDetailBO.setQty(soDetail.getQty()); //数量
            orderDetailBO.setProductionDate(DateUtils.getNowDateString());
            orderDetailBO.setStockId(soDetail.getStockId()); //
            details.add(orderDetailBO);
        }
        orderToErpBO.setDetails(details);
        return orderToErpBO;
    }

    /**
     * 字段添加
     *  @param orderToErpBO
     * @param
     * @param asnMap
     */
    protected abstract void add(OrderToErpBO orderToErpBO, SoMaster soMaster, Map<String, SoDetail> asnMap);

    /**
     * 回传ERP
     * @param orderToErpBO
     */
    protected abstract void pushErp(OrderToErpBO orderToErpBO);


    /**
     * 方法汇总
     * @param
     * @param
     * @param
     */
    public void sendERP(SoMaster soMaster, List<SoDetail> soDetails) {
        //Step0. 前置操作
        if (!this.front(soMaster, soDetails)) {
            return;
        }
        //Step1.抽象类实现字段映射
        OrderToErpBO orderToErpBO = this.EnterBeanAndPush(soMaster, soDetails);
        //Step2.子类实现特殊字段添加
        Map<String, SoDetail> asnMap = convertMap(soDetails, SoDetail::getDetailNo);
        this.add(orderToErpBO, soMaster, asnMap);
        //Step3. 子类实现回传ERP
        this.pushErp(orderToErpBO);
    }
}
